<?php

/**
* Post Fields - A plugin to display additional fields on the publish page
* @package postfields
**/
class postfields extends Plugin
{


	const VERSION= '1.1';

 /**
   * Add update beacon support
   **/
  public function action_update_check()
  {
		Update::add( 'Postfields', '228D6060-38F0-11DD-AE16-0800200C9A66', $this->info->version );
  }


	/**
	* Add actions to the plugin page for this plugin
	*
	* @param array $actions An array of actions that apply to this plugin
	* @param string $plugin_id The string id of a plugin, generated by the system
	* @return array The array of actions to attach to the specified $plugin_id
	*/
	public function filter_plugin_config($actions, $plugin_id)
	{
		if ($plugin_id == $this->plugin_id()){
			$types = Post::list_active_post_types();
			unset($types['any']);
			foreach($types as $type => $id) {
				$actions['config-' . $id] = _t('%s fields', array($type));
			}
			//$actions['plugin'] = _t('Build Plugin');
		}

		return $actions;
	}

	/**
	* Respond to the user selecting an action on the plugin page
	*
	* @param string $plugin_id The string id of the acted-upon plugin
	* @param string $action The action string supplied via the filter_plugin_config hook
	*/
	public function action_plugin_ui($plugin_id, $action)
	{
		if ($plugin_id == $this->plugin_id()) {
			switch($action) {
				/*
				case 'plugin':
					$ui = new FormUI('postfields');
					$ui->append('static', 'typelabel', _t('Add this code to a plugin to implement the currently configured fields.'));
					$ui->append('textarea', 'plugincode', 'null:null', _t('Plugin code:'))->value = $this->get_code();
					$ui->out();
					break;
				*/
				default:
					$types = array_flip(Post::list_active_post_types());
					$key = substr($action, 7);
			
					$ui = new FormUI('postfields');
					$ui->append('static', 'typelabel', _t('Adding fields to the "%s" post type.', array($types[$key])));
					
					$ui->append('hidden', 'postfields_type', 'null:null');
					$ui->postfields_type->value = $key;
					
					$postfields = $ui->append('fieldset', "postfields", _t('Postfields'));
					
					$values = Options::get( "postfields_{$key}" );

					if( is_array( $values ) && !empty( $values ) ) {	
						foreach( $values as $i => $value ) {
						
							$temp = "settings{$i}";
							$$temp = $postfields->append('fieldset', "settings{$i}", _t('Extra field'));
							$$temp->append('text', "settings{$i}_field", "null:null", 'Fieldname:');
							$$temp->append('select', "settings{$i}_type", "null:null", 'Type:');
							$type = "settings{$i}_type";
	    					$ui->$type->options = array('admincontrol_text' => 'Input field',
	    												'admincontrol_textarea' => 'Textarea',
	    												'tabcontrol_text' => 'Tab text',
	    												'tabcontrol_checkbox' => 'Tab checkbox');

							$$temp->append('select', "settings{$i}_position", "null:null", 'Position:');
							$type = "settings{$i}_position";
	    					$ui->$type->options = array('Before Title', 'After Title', 'Before Content', 'After Content', 'Seperate tab');
	    					
							$$temp->append('checkbox', "settings{$i}_delete", 'null:null', _t('Delete') );    					
						}
					}
					
					$submit = $ui->append('submit', 'submit', 'Save');

					$ui->append('checkbox', "add_new_field", "null:null", 'Add new field:');
					
					$ui->on_success(array($this, 'my_callback'));

					$ui->out();
			}
		}
	}
	
	public function my_callback( $form )
	{
		$values = array();

		$types = array_flip(Post::list_active_post_types());
		$key = $form->controls['postfields_type']->value;

  		foreach( $form->controls['postfields']->controls as $control ) {
  			if( $control->controls[$control->name . '_delete']->value != 1 ) {
  				$values[] = array( 'field' => $control->controls[$control->name . '_field']->value,
  									'type' => $control->controls[$control->name . '_type']->value,
  									'position' => $control->controls[$control->name . '_position']->value );
			}
  		}

		if( $form->controls['add_new_field']->value == 1 ) {
			$values[] = array( 'field' => null, 'type' => null, 'position' => null );
			Session::notice(_t('Added new field'));
		}

  		sort($values);
		Options::set( "postfields_{$key}", $values );
		Session::notice(_t('Saving'));
  		return false;
	}

	/**
	* Add additional controls to the publish page tab
	*
	* @param FormUI $form The form that is used on the publish page
	* @param Post $post The post being edited
	**/
	public function action_form_publish($form, $post)
	{

		$fields = Options::get( 'postfields_' . $post->content_type );
		
		if(!is_array($fields) || count($fields) == 0) {
			return;
		}

		$postfields = $form->publish_controls->append('fieldset', 'postfields', 'Additional Fields');

		foreach( $fields as $id => $field ) {
			
			if( empty( $field['type'] ) ) continue;
			
			$type = explode( '_', $field['type'] );
			$control_id = md5($field['field'] + $id );
			$fieldname = "postfield_{$control_id}";

			$form->append( $type[1], $fieldname, 'null:null',_t( $field['field'] ), $type[0] . "_" . $type[1] );
			$form->$fieldname->value = ( isset( $post->info->$field['field'] ) ? $post->info->$field['field'] : '' );	

			switch( $field['position'] )
			{
				case '0':
					$form->move_before( $form->$fieldname, $form->title );
					break;
				case '1':
					$form->move_after( $form->$fieldname, $form->title );
					break;
				case '2':
					$form->move_before( $form->$fieldname, $form->content );
					break;			
				case '3':
					$form->move_after( $form->$fieldname, $form->content );
					break;
				case '4':
					$form->move_into( $form->$fieldname, $form->postfields );
					break;
			}
		}
	}

	/**
	* Modify a post before it is updated
	*
	* @param Post $post The post being saved, by reference
	* @param FormUI $form The form that was submitted on the publish page
	*/
	public function action_publish_post($post, $form)
	{
		$fields = Options::get('postfields_' . $post->content_type);
		if(!is_array($fields) || count($fields) == 0) {
			return;
		}

		foreach($fields as $id => $field) {
			$name = $field['field'];
			$control_id = md5($name + $id );
			$fieldname = "postfield_{$control_id}";
			$customfield = $form->$fieldname;
			$post->info->$name = $customfield->value;
		}
	}

	public function get_code()
	{
		$cases_form = '';

		$types = Post::list_active_post_types();
		unset($types['any']);
		foreach($types as $type => $id) {
			$fields = Options::get('postfields__fields_' . $id);
			if(!is_array($fields) || count($fields) == 0) {
				continue;
			}
			$fieldlist = array();
			foreach($fields as $field) {
				$fieldlist[] = "'" . addslashes($field) . "'";
			}
			$fieldlist = implode(', ', $fieldlist);
			$cases_form .= "\t\t\tcase {$id}:\n\t\t\t\t\$fields = array({$fieldlist});\n\t\t\t\tbreak;\n";
		}		

		$code = <<< PLUGIN_CODE_1

	/**
	* Add additional controls to the publish page tab
	*
	* @param FormUI \$form The form that is used on the publish page
	* @param Post \$post The post being edited
	**/
	public function action_form_publish(\$form, \$post)
	{
		switch(\$post->content_type) {
			{$cases_form}
			default:
				return;
		}
		foreach(\$fields as \$field) {
			\$control_id = md5(\$field);
			\$fieldname = "postfield_{\$control_id}";
			\$customfield = \$postfields->append('text', \$fieldname, 'null:null', \$field);
			\$customfield->value = isset(\$post->info->{\$field}) ? \$post->info->{\$field} : '';
			\$customfield->template = 'tabcontrol_text';
		}
	}
	

	/**
	* Modify a post before it is updated
	*
	* @param Post \$post The post being saved, by reference
	* @param FormUI \$form The form that was submitted on the publish page
	*/
	public function action_publish_post(\$post, \$form)
	{
		switch(\$post->content_type) {
			{$cases_form}
			default:
				return;
		}
		foreach(\$fields as \$field) {
			\$control_id = md5(\$field);
			\$fieldname = "postfield_{\$control_id}";
			\$customfield = \$form->\$fieldname;
			\$post->info->{\$field} = \$customfield->value;
		}
	}

PLUGIN_CODE_1;

		return $code;		
	}
	
		
 

}
?>